import router, { asyncRoutes, constantRoutes } from '@/router'

const state = () => {
  return {
    allRoutes: constantRoutes
  }
}

const mutations = {
  setRoutes(state, newRoutes) {
    state.allRoutes = newRoutes
  }
}
const actions = {
  filterRoutes(context) {
    const _menus = context.rootState.user.userInfo.roles.menus
    const _newRoutes = asyncRoutes.filter(v => {
      v.children = v.children.filter(vs => {
        return _menus.includes(vs.name)
      })
      if (v.children.length > 0) {
        return true
      } else {
        return false
      }
    })
    router.addRoutes(_newRoutes)
    context.commit('setRoutes', [...constantRoutes, ..._newRoutes])
  },
  clear(context) {
    context.commit('setRoutes', [])
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
